package com.panshi.controller;

import com.panshi.api.bean.ArticleReviewsBean;
import com.panshi.api.util.PageSizeVO;
import com.panshi.pojo.*;
import com.panshi.servers.ArticleService;
import com.panshi.servers.ReviewsServers;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @author XiaoWangYa
 * @version 1.0
 * @date 2020/8/4 0004 下午 3:28
 */
@Slf4j
@Controller
@ApiModel(" 文章评论Controller层 ")
public class ReviewsController {

    @Autowired  // 评论
    private ReviewsServers yb_reviewsServers;

    @Autowired  // 文章
    private ArticleService articleService;

    @PostMapping("/addReviewsInfo")
    @ResponseBody
    @ApiModelProperty("新增文章评论")
    public ArticleReviewsBean addReviewsInfo (@RequestBody ArticleReviewsVO yb_ArticleReviews_VO) {
        log.info("新增文章评论 --> " + yb_ArticleReviews_VO);
        int id = yb_reviewsServers.addReviewsInfo(yb_ArticleReviews_VO);
        // 获取新增评论数据
        ArticleReviewsVO articleReviewsVO = yb_reviewsServers.queryArticleParise(yb_ArticleReviews_VO.getArid());
        System.out.println(yb_ArticleReviews_VO.getArid() + "   评论自增Id");
        if (yb_ArticleReviews_VO.getParentid() == 0) {  // 评论
            // 获取所查询的文章信息
            ArticleVO yb_article_vo = articleService.queryAssignArticleInfo(yb_ArticleReviews_VO.getAid());
            // 指定文章评论数 +1
            articleService.addCount(yb_article_vo.getAid());
            // 搜索表评论数 +1
            articleService.addSearchCount(yb_article_vo.getAid());

            // 数据转换
            ArticleReviewsBean reviews = new ArticleReviewsBean();
            BeanUtils.copyProperties(articleReviewsVO,reviews);
            // 将时间进行转换为xxx前发布
            String timeFormatText = TimeVO.getTimeFormatText(articleReviewsVO.getAr_time());
            reviews.setDateTime(timeFormatText);
            return reviews;
        } else {    // 回复再回复
            PageSizeVO page = new PageSizeVO();
            page.setAid(articleReviewsVO.getArid());
            // 查询回复信息
            List<ArticleReviewsVO> articleReviewsVOS = yb_reviewsServers.queryAssginArticleReplyInfo(page);
            // 数据转换
            ArticleReviewsBean reviews = new ArticleReviewsBean();
            BeanUtils.copyProperties(articleReviewsVOS.get(0),reviews);
            // 将时间进行转换为xxx前发布
            String timeFormatText = TimeVO.getTimeFormatText(articleReviewsVO.getAr_time());
            reviews.setDateTime(timeFormatText);
            return reviews;
        }
    }

    @PostMapping("/deleteReviewsInfo/{id}")
    @ResponseBody
    @ApiModelProperty("删除评论")
    public String deleteReviewsInfo (@PathVariable int id) {
        log.info("删除评论 --> " + id);
        yb_reviewsServers.deleteReviewsInfo(id);
        return "ok";
    }

    @PostMapping("/addArticlePraise")
    @ResponseBody
    @ApiModelProperty("点赞评论")
    public int addArticlePraise (@RequestBody PraoseLinkVO praoseLinkVO) {
        log.info("点赞评论 --> " + praoseLinkVO);
        // 查询是否被指定用户点赞过
        PraoseLinkVO praoseLink = yb_reviewsServers.queryPraiseLink(praoseLinkVO);
        // 查询指定评论信息
        ArticleReviewsVO articleReviewsVO = yb_reviewsServers.queryArticleParise(praoseLinkVO.getRid());
        if (praoseLink != null) {
            /*if (praoseLinkVO.getState() == 0) {
                log.info("取消评论点赞");
                // 点赞 -1
                articleReviewsVO.setRead_like(articleReviewsVO.getRead_like() - 1);
                // 减少评论点赞量
                yb_reviewsServers.addArticlePraise(articleReviewsVO);
                // 取消评论点赞数据
                log.info("点赞评论 --> " + praoseLinkVO);
                yb_reviewsServers.cancelArticlePraise(praoseLinkVO);
                return "ok";
            } else if (praoseLinkVO.getState() == 1) {
                log.info("评论点赞");
                // 点赞 +1
                articleReviewsVO.setRead_like(articleReviewsVO.getRead_like() + 1);
                articleReviewsVO.setState(praoseLink.getState());
                // 增加评论点赞量
                yb_reviewsServers.addArticlePraise(articleReviewsVO);
                // 修改评论点赞数据
                yb_reviewsServers.cancelArticlePraise(praoseLinkVO);
                return "ok";
            } else {
                return "ok";
            }*/
            return articleReviewsVO.getRead_like();
        } else {
            log.info("新增评论点赞数据");
            articleReviewsVO.setRead_like(articleReviewsVO.getRead_like() + 1);
            System.out.println(articleReviewsVO.getRead_like());
            // 点赞 +1
            yb_reviewsServers.addArticlePraise(articleReviewsVO);
            // 增加点赞数据
            yb_reviewsServers.addLinkInfo(praoseLinkVO);
            return articleReviewsVO.getRead_like();
        }
    }

    @PostMapping("/queryAssginArticleReviewsInfo")
    @ResponseBody
    @ApiModelProperty("查询所属评论文章")
    public List<ArticleReviewsBean> queryAssginArticleReviewsInfo (@RequestBody PageSizeVO yb_PageSize_VO) {
        log.info("查询所属评论文章 --> " + yb_PageSize_VO);
        // 设置分页初始页
        yb_PageSize_VO.setStartIndex(yb_PageSize_VO.getStartIndexInfo());
        // 获取文章下所有评论数据
        List<ArticleReviewsVO> list = yb_reviewsServers.queryAssginArticleReviewsInfo(yb_PageSize_VO);
        List<ArticleReviewsBean> bean = new ArrayList<>();
        for (ArticleReviewsVO articleReviewsVO : list) {
            ArticleReviewsBean reviews = new ArticleReviewsBean();
            // 数据转换
            BeanUtils.copyProperties(articleReviewsVO,reviews);
            // 将时间进行转换为xxx前发布
            String timeFormatText = TimeVO.getTimeFormatText(articleReviewsVO.getAr_time());
            reviews.setDateTime(timeFormatText);
            bean.add(reviews);
        }
        return bean;
    }

    @PostMapping("/queryAssginArticleReplyInfo")
    @ResponseBody
    @ApiModelProperty("查询评论回复信息")
    public List<ArticleReviewsBean> queryAssginArticleReplyInfo (@RequestBody PageSizeVO yb_PageSize_VO) {
        log.info("查询所属评论文章 --> " + yb_PageSize_VO);
        // 设置分页初始页
        yb_PageSize_VO.setStartIndex(yb_PageSize_VO.getStartIndexInfo());
        // 获取评论下所有回复数据
        List<ArticleReviewsVO> list = yb_reviewsServers.queryAssginArticleReplyInfo(yb_PageSize_VO);
        List<ArticleReviewsBean> bean = new ArrayList<>();
        for (ArticleReviewsVO articleReviewsVO : list) {
            ArticleReviewsBean reviews = new ArticleReviewsBean();
            // 数据转换
            BeanUtils.copyProperties(articleReviewsVO,reviews);
            // 将时间进行转换为xxx前发布
            String timeFormatText = TimeVO.getTimeFormatText(articleReviewsVO.getAr_time());
            reviews.setDateTime(timeFormatText);
            bean.add(reviews);
        }
        return bean;
    }

}
